package com.yongz.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yongz.domain.Account;
import com.yongz.domain.Stu;
import com.yongz.service.AccountService;
import com.yongz.service.StuService;
import com.yongz.vo.StuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
public class StubBizImpl implements StuBiz {
    @Autowired
    private StuService stuService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<StuVo> getAllStuVo() {
        List<StuVo> stuVoList = new ArrayList<>();
        for (Stu stu : stuService.list()) {
            StuVo stuVo = new StuVo();
            stuVo.setId(stu.getId());
            stuVo.setName(stu.getName());
            stuVo.setAge(stu.getAge());
            stuVo.setEmail(stu.getEmail());
            stuVo.setClassroom(stu.getClassroom());
            Account account = accountService.getById(stu.getId());
            if (account == null) {
                throw new RuntimeException("找不到对应的账户");
            }
            stuVo.setUsername(account.getUsername());
            stuVo.setPassword(account.getPassword());
            stuVoList.add(stuVo);
        }
        return stuVoList;
    }

    @Override
    public StuVo getStuVoById(Integer id) {
        Stu stu = stuService.getById(id);
        if (stu == null) {
            throw new RuntimeException("找不到对应的学生");
        }
        StuVo stuVo = new StuVo();
        stuVo.setId(stu.getId());
        stuVo.setName(stu.getName());
        stuVo.setAge(stu.getAge());
        stuVo.setEmail(stu.getEmail());
        stuVo.setClassroom(stu.getClassroom());
        Account account = accountService.getById(id);
        if (account == null) {
            throw new RuntimeException("找不到对应的账户");
        }
        stuVo.setUsername(account.getUsername());
        stuVo.setPassword(account.getPassword());
        return stuVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StuVo addStuVo(StuVo stuVo) {
        Stu stu = new Stu();
        stu.setId(stuVo.getId());
        stu.setName(stuVo.getName());
        stu.setAge(stuVo.getAge());
        stu.setEmail(stuVo.getEmail());
        stu.setClassroom(stuVo.getClassroom());
        boolean isSuccess = stuService.save(stu);
        if (!isSuccess) {
            throw new RuntimeException("添加学生失败");
        }
        Account account = new Account();
        account.setId(stu.getId());
        account.setEmail(stu.getEmail());
        account.setUsername(stuVo.getUsername());
        account.setPassword(stuVo.getPassword());
        isSuccess = accountService.save(account);
        if (!isSuccess) {
            throw new RuntimeException("添加账户失败");
        }
        return stuVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStuVoById(Integer id) {
        Stu stu = stuService.getById(id);
        if (stu == null) {
            throw new RuntimeException("找不到对应的学生");
        }
        boolean isSuccess = stuService.removeById(id);
        if (!isSuccess) {
            throw new RuntimeException("删除学生失败");
        }
        Account account = accountService.getById(id);
        if (account == null) {
            throw new RuntimeException("找不到对应的账户");
        }
        isSuccess = accountService.removeById(id);
        if (!isSuccess) {
            throw new RuntimeException("删除账户失败");
        }
        return isSuccess;
    }

    public static String stuKey = "test:";

    // 利用redis缓存
    @Override
    public StuVo getStuVoWithRedis(Integer id) {
        String key = stuKey + id;
        StuVo stuVo = (StuVo) redisTemplate.opsForValue().get(key);
        if (stuVo == null) {
            Stu stu = stuService.getById(id);
            if (stu == null) {
                throw new RuntimeException("找不到对应的学生");
            }
            stuVo = new StuVo();
            stuVo.setId(stu.getId());
            stuVo.setName(stu.getName());
            stuVo.setAge(stu.getAge());
            stuVo.setEmail(stu.getEmail());
            stuVo.setClassroom(stu.getClassroom());
            Account account = accountService.getById(id);
            if (account == null) {
                throw new RuntimeException("找不到对应的账户");
            }
            stuVo.setUsername(account.getUsername());
            stuVo.setPassword(account.getPassword());
            redisTemplate.opsForValue().set(key, stuVo, 1, TimeUnit.MINUTES);
        }
        return stuVo;
    }

    @Override
    public StuVo getStuVoWithUsername(String Username) {
        String key = stuKey + Username;
        String id = stringRedisTemplate.opsForValue().get(key);
        if (id == null) {
            Account account = accountService.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUsername, Username).last("limit 1"));
            if (account == null) {
                throw new RuntimeException("找不到对应的账户");
            }
            id = account.getId().toString();
            stringRedisTemplate.opsForValue().set(key, id, 1, TimeUnit.MINUTES);
        }
        return getStuVoWithRedis(Integer.parseInt(id));
    }


    @Override
    public boolean checkUsername(String username,String password) {
        Account account = accountService.getOne(new LambdaQueryWrapper<Account>().eq(Account::getUsername, username).last("limit 1"));
        if (account == null) {
            throw new RuntimeException("找不到对应的账户");
        }
        if (account.getPassword().equals(password)){
            return true;
        }
        return false;
    }
}
